using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;
using System.Threading;

using Wrath.Configuration;

namespace Wrath.RuleForge
{
    public partial class SplashScreen : Form
    {
        public SplashScreen()
        {
            InitializeComponent();

            listBox.Items.Clear();
        }

        public void Run()
        {
            DialogResult = DialogResult.OK;

            // Get Fantasy Grounds Data Directory            
            Write("Finding Fantasy Grounds Data Directory...");

            RegistryKey dataFolder = Registry.CurrentUser.OpenSubKey("Software\\Fantasy Grounds\\2.0", false);
            if (dataFolder == null)
            {
                dataFolder = Registry.CurrentUser;
                Write("  Error opening Fantasy Grounds registry settings.");
            }
            string dataDir = (string)dataFolder.GetValue("DataDir", null);
            if (String.IsNullOrEmpty(dataDir))
            {
                Globals.FantasyGroundsPaths.Data = SelectFantasyGroundsDataDirectory();
                if (Globals.FantasyGroundsPaths.Data == null)
                {
                    DialogResult = DialogResult.Cancel;
                    Close();
                    return;
                }                
            }
            else
            {
                Globals.FantasyGroundsPaths.Data = dataDir;
            }

            string pathOutput = (Globals.FantasyGroundsPaths.Data.Length > 35) ? Globals.FantasyGroundsPaths.Data.Substring(0, 35) + "..." : Globals.FantasyGroundsPaths.Data;
            Write("  " + pathOutput);

            Globals.Config.SetSetting("RuleForge", "FantasyGroundsDataPath", Globals.FantasyGroundsPaths.Data);
            Globals.Config.Save();

            Write("");

            // Find RuleSets
            MainForm mainForm = (MainForm)FormBroker.GetForm("main");

            Write("Searching for RuleSets...");

            Globals.FantasyGroundsPaths.RuleSets = Globals.FantasyGroundsPaths.Data + "\\rulesets";
            string[] rulesets = Directory.GetDirectories(Globals.FantasyGroundsPaths.RuleSets);

            if (rulesets.Length < 1)
            {
                Write(" None found...");
            }

            foreach (string ruleset in rulesets)
            {
                if (File.Exists(ruleset + "\\base.xml"))
                {
                    string ruleSetName = Path.GetFileName(ruleset);
                    Write(" * " + ruleSetName);
                    
                    mainForm.AddRuleSet(ruleSetName);
                }
            }

            Write("");

            // Find Modules
            Write("Searching for Modules...");
            Globals.FantasyGroundsPaths.Modules = Globals.FantasyGroundsPaths.Data + "\\modules";
            string[] modules = Directory.GetFiles(Globals.FantasyGroundsPaths.Modules, "*.mod");

            if (modules.Length < 1)
            {
                Write(" None found...");
            }

            foreach (string module in modules)
            {
                string moduleName = Path.GetFileName(module);

                if (!mainForm.AddModule(moduleName))
                {
                    Write(" * " + moduleName + " (corrupt/broken)");
                }
                else
                {
                    Write(" * " + moduleName + " (cached)");
                }
            }

            Close();
        }

        private void Write(string text)
        {
            listBox.Items.Add(text);
            Application.DoEvents();
        }

        private string SelectFantasyGroundsDataDirectory()
        {
            string returnPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            DialogResult result = DialogResult.OK;

            string configPath = Globals.Config.GetSetting("RuleForge","FantasyGroundsDataPath");
            if (String.IsNullOrEmpty(configPath))
            {
                result = DialogResult.None;
            }
            else
            {
                if (!(Directory.Exists(configPath + "\\rulesets") && Directory.Exists(configPath + "\\modules")))
                {
                    result = DialogResult.None;
                }
                else
                {
                    returnPath = configPath;
                }
            }
             
            while (result == DialogResult.None)
            {
                result = folderBrowserDialog.ShowDialog();
                if (result == DialogResult.OK)
                {
                    if (!(Directory.Exists(folderBrowserDialog.SelectedPath + "\\rulesets") && Directory.Exists(folderBrowserDialog.SelectedPath + "\\modules")))
                    {
                        MessageBox.Show("The selected folder is not a valid Fantasy Grounds Data Directory!", "Invalid Path", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        result = DialogResult.None;
                    }
                    else
                    {
                        returnPath = folderBrowserDialog.SelectedPath;
                    }
                }
            }

            if (result == DialogResult.Cancel)
            {
                return null;
            }            

            return returnPath;
        }
    }
}